import { defineStore } from 'pinia'
export const homeStore = defineStore('main', {
  state() {
    return {
      tabList: [],
      // 当前选项卡id,也就是path
      currentContextTabId: ''
    }
  },
  actions: {
    // 添加rab
    addTab(ItemTab) {
      const isSome = this.tabList.some(item => item.path == ItemTab.path)
      if (!isSome) {
        this.tabList.push(ItemTab)
      }
    },
    // 删除tab
    closeCurrentTab(targetName) {
      const index = this.tabList.findIndex(item => item.path === targetName)
      this.tabList.splice(index, 1)
    },
    // 刷新保存页面的tab数据
    refresh(list) {
      this.tabList = list
    },
    // 保留当前id{
    saveCurrentContextTabId(id) {
      this.currentContextTabId = id
    },
    // 关闭所有
    closeAllTabs() {
      this.tabList = []
      sessionStorage.removeItem('tabs_routes')
    },
    //关闭其他
    closeOtherTabs(type) {
      if (type === 'other') {
        // 列表里只留下当前id,也就是当前path
        this.tabList = this.tabList.filter(item => item.path == this.currentContextTabId)
      } else if (type === 'left') {
        console.log(type);
        //如果是删除左边,那就比如第三个,索引是2,那么就slice
        let index = this.tabList.findIndex(item => item.path == this.currentContextTabId)
        // 比如当前第三个, 从0开始删, 删2个
        this.tabList.splice(0, index)
      } else if (type === 'right') {
        console.log(type);
        let index = this.tabList.findIndex(item => item.path == this.currentContextTabId)
        // 比如当前第三个, 从index开始删,也就是2,所以要加1
        this.tabList.splice(index + 1)
      }
    }

  },
  getters: {
    getAddTab() {
      return this.tabList
    }
  }
})